﻿using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SageKing.Database.Contracts;

namespace SageKing.UI.Blazor.SageKingAntDesign.Contracts;

public interface IModalCRUD<Tmodel, Tcomponent, Trow, Tparent> : IModal<Tmodel>
    where Tcomponent : IComponent
    where Tmodel : ICloneable
{
    public IModalCRUD<Tmodel, Tcomponent, Trow, Tparent> MeCrud { get; }
     
    /// <summary>
    /// 视图组件
    /// </summary>
    public Tcomponent Component { get; set; }
     
    #region CRUD
    public virtual async Task ShowAdd(Trow id, bool isinitMoel = true)
    {
        Visible = false;
        if (isinitMoel)
        {
            Model = (Tmodel)Activator.CreateInstance(typeof(Tmodel));
        }
        else
        {
            var result = await InitModel(id);
            if (!result)
            {
                Visible = false;
                return;
            }
        }
        Title = $"#{TabTitle}-{ParentName}-:>新增";
        Visible = true;
        Loading = false;
        await Task.CompletedTask;
    }

    public virtual async Task ShowEdit(Tmodel row)
    {
        Visible = false;
        Model = (Tmodel)row.Clone();
        Title = $"#{TabTitle}-{ParentName}-:>编辑";
        Visible = true;
        Loading = false;
        await Task.CompletedTask;
    }

    public Task DeleteData(Tmodel row, Tparent parent);
      
    #endregion
    /// <summary>
    /// 用于ShowAdd Model 初启化
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Task<bool> InitModel(Trow id);
}
